package cn.aran.sys.controller;

import cn.aran.basic.annotation.PreAuthorize;
import cn.aran.basic.utils.AjaxResult;
import cn.aran.basic.utils.PageList;
import cn.aran.sys.domain.DictionaryType;
import cn.aran.sys.query.DictionaryTypeQuery;
import cn.aran.sys.service.IDictionaryTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "数据字典类型的API", description = "数据字典类型相关的CRUD功能")
@RestController
@RequestMapping("/dictionaryType")
public class DictionaryTypeController {
    @Autowired
    private IDictionaryTypeService dictionaryTypeService;

    /**
     * @Title: loadById
     * @Description: 根据ID查询数据字典类型
     * @Author: Aran
     * @Version: 1.0
     * @Date:  2022/6/26 14:55
     * @Parameters: [id]
     * @Return cn.aran.sys.domain.DictionaryType
     */
    @ApiOperation(value = "通过ID查询")
    @GetMapping("/{id}")
    public DictionaryType loadById(@ApiParam(value = "主键ID属性", required = true) @PathVariable("id") Long id) {
        return dictionaryTypeService.loadById(id);
    }

    /**
     * @Title: loadAll
     * @Description: 查询数据字典类型所有
     * @Author: Aran
     * @Version: 1.0
     * @Date:  2022/6/26 14:55
     * @Parameters: []
     * @Return java.util.List<cn.aran.sys.domain.DictionaryType>
     */
    @ApiOperation(value = "查询全部")
    @GetMapping
    public List<DictionaryType> loadAll() {
        List<DictionaryType> dictionaryTypes = dictionaryTypeService.loadAll();
        return dictionaryTypes;
    }

    /**
     * @Title: remove
     * @Description: 根据id删除数据字典类型
     * @Author: Aran
     * @Version: 1.0
     * @Date:  2022/6/26 14:56
     * @Parameters: [id]
     * @Return cn.aran.basic.util.AjaxResult
     */
    @ApiOperation(value = "数据字典类型删除")
    @DeleteMapping("/{id}")
    @PreAuthorize(name = "删除数据字典", sn = "dictionaryType:delete")
    public AjaxResult remove(@ApiParam(value = "主键ID属性", required = true) @PathVariable("id") Long id) {
        try {
            dictionaryTypeService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,请稍后重试!");
        }

    }

    /**
     * @Title: batchRemove
     * @Description: 数据字典类型批量删除
     * @Author: Aran
     * @Version: 1.0
     * @Date:  2022/6/26 14:56
     * @Parameters: [ids]
     * @Return cn.aran.basic.util.AjaxResult
     */
    @PatchMapping
    @PreAuthorize(name = "批量删除数据字典", sn = "dictionaryType:patch")
    public AjaxResult batchRemove(@RequestBody List<Long> ids) {
        try {
            dictionaryTypeService.batchRemove(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,请稍后重试!");
        }

    }

    /**
     * @Title: saveOrUpt
     * @Description: 新增修改数据字典类型
     * @Author: Aran
     * @Version: 1.0
     * @Date:  2022/6/26 14:56
     * @Parameters: [dictionaryType]
     * @Return cn.aran.basic.util.AjaxResult
     */
    @ApiOperation(value = "数据字典类型新增或修改")
    @PutMapping
    @PreAuthorize(name = "保存数据字典", sn = "dictionaryType:save")
    public AjaxResult saveOrUpt(@RequestBody DictionaryType dictionaryType) {
        try {
            if (dictionaryType.getId() == null) {
                dictionaryTypeService.save(dictionaryType);
            } else {
                dictionaryTypeService.update(dictionaryType);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,请稍后重试!");
        }
    }

    /**
     * @Title: queryData
     * @Description: 数据字典类型高级查询,分页
     * @Author: Aran
     * @Version: 1.0
     * @Date:  2022/6/26 14:56
     * @Parameters: [dictionaryTypeQuery]
     * @Return cn.aran.basic.util.PageList<cn.aran.sys.domain.DictionaryType>
     */
    @ApiOperation(value = "数据字典类型高级查询和分页")
    @PostMapping
    @PreAuthorize(name = "列表数据字典", sn = "dictionaryType:list")
    public PageList<DictionaryType> queryData(@RequestBody DictionaryTypeQuery dictionaryTypeQuery) {
        PageList<DictionaryType> data = dictionaryTypeService.queryData(dictionaryTypeQuery);
        return data;
    }

}
